package Servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.Session;

import utilidades.HtmlGen;
import utilidades.Validador;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class Login extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	private ServletContext sc;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
    }

    public void init(ServletConfig config) throws ServletException{
		
		super.init(config);
		sc = getServletContext();
		
    }
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			HttpSession session = request.getSession(true);
			Validador val = new Validador(sc);
			boolean respuesta = false;
			
			String uid 	= (String) request.getParameter("username");
			String pass = (String) request.getParameter("password");
			
			if(uid != null && pass != null){
				respuesta = val.validar(uid, pass);
			}
			else{
				response.setHeader("Cache-Control", "no-cache, no-store");
				response.setHeader("Pragma", "no-cache"); 
							 
				response.sendRedirect(request.getContextPath() + "/loginReIntent.html");
				return;
			}
			
			
			if(respuesta){
				session.setAttribute("user", uid);
				session.setAttribute("pass", pass);
			}
			
			else{
				response.setHeader("Cache-Control", "no-cache, no-store");
				response.setHeader("Pragma", "no-cache"); 
							 
				response.sendRedirect(request.getContextPath() + "/loginReIntent.html");
				return;
			}
				
			PrintWriter out;
			out = response.getWriter();
			   
			response.setContentType("text/html");
			  		
			out.println("<html>");
			out.println("<head><title>Resultado Login</title></head>");
			
			String urlEstilo = request.getContextPath() + "/css/cv.css"; 
			out.println("<link rel=\"stylesheet\" href=" + urlEstilo + ">");
			out.println("<meta http-equiv=\"content-type\" lang=\"es\" " +
					"content=\"text/html; charset=utf-8\"/>");

			out.println("</head>");
			
			out.println("<body>");
			
			if(respuesta){
				System.out.println("respuesta: "+ respuesta );
				HtmlGen.escribeCabeceraLogin(out);
			}
			else{
				HtmlGen.escribeCabecera(out);
			}
			
			out.println("<div id=\"contenedor\">");
			out.println("<div id=\"Inscripcion\" class=\"apartado_cv\">");
			
			if(respuesta){
				out.println("<h2>Login correcto</h2>");
				out.println("<img id=\"foto\" class=\"correcto\" src=/Practica4Pawii/css/loginCorrecto.png alt=\"login correcto\" >");
			}
			else{
				out.println("\"<div id=\"inscripcion\" class=\"apartado_cv\">");
					out.println("<h2 class=\"title\">");
						out.println("Iniciar sesión");
					out.println("</h2>");
					
					out.println("<p>");
						out.println("<form action=\"http://localhost:8080/Practica4Pawii/Principal\" method=\"post\">");
							
							out.println("<p>Username:</p>");
							out.println("<p><input type=\"text\" name=\"username\" size=\"50\"required=\"required\"></p>");
							out.println("<p>Password:</p>");
							
							out.println("<p><input type=\"password\" name=\"password\"size=\"50\" required=\"required\"></p>");
							out.println("<input type=\"hidden\" name=\"accion\">");
							out.println("<p><input type=\"submit\" name=\"accion\" value=\"login\"></p>");
							
						out.println("</form>");
					out.println("</p>");
								
					out.println("<p>");
						out.println("<a href=\"http://localhost:8080/Practica4Pawii/Inscripciones\"> " +
								"¿Ha olvidado su contraseña?</a>");
						
					out.println("</p>");
				out.println("</div>");
			}
			out.println("</body></html>");
		}
		catch(Exception e){
			response.setHeader("Cache-Control", "no-cache, no-store");
			response.setHeader("Pragma", "no-cache"); 
						 
			response.sendRedirect(request.getContextPath() + "/ErrorPages/unknow.html");
		}
	}
		
}
